package com.myapp.ui;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.myapp.domain.OrderRequest;
import com.myapp.net.HttpDoPostUtils;

public class IndexAction extends BaseAction {
	private static String trainQueryUrl = "http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryststrainall";
	private OrderRequest orderRequest;
	private String date;
	private String fromstation;
	private String tostation;
	private String starttime;
	
	public String index(){
		return SUCCESS;
	}
	
	public String ticketsQuery(){
		StringBuffer url = new StringBuffer("ttp://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryLeftTicket&");
		return SUCCESS;
	}

	public String cities(){
		return SUCCESS;
	}
	
	public String trainQuery(){
		StringBuffer sb = new StringBuffer(trainQueryUrl);
		sb=sb.append("&").append("date=").append(date);
		sb.append("&").append("fromstation=").append(fromstation);
		sb.append("&").append("tostation=").append(tostation);
		sb.append("&").append("starttime=").append(starttime);
		try {
			String str = HttpDoPostUtils.doRequestToString(sb.toString(), null, session);
			Map<String,Object> result = new HashMap<String,Object>();
			result.put("data", JSONArray.fromObject(str));
			toVm(result);
//			response.getWriter().write(JSONObject.fromObject(result).toString());
//			response.flushBuffer();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public OrderRequest getOrderRequest() {
		return orderRequest;
	}

	public void setOrderRequest(OrderRequest orderRequest) {
		this.orderRequest = orderRequest;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getFromstation() {
		return fromstation;
	}

	public void setFromstation(String fromstation) {
		this.fromstation = fromstation;
	}

	public String getTostation() {
		return tostation;
	}

	public void setTostation(String tostation) {
		this.tostation = tostation;
	}

	public String getStarttime() {
		return starttime;
	}

	public void setStarttime(String starttime) {
		this.starttime = starttime;
	}
}
